FOUNDATION OF MACHINE LEARNING¶

Practical Lab¶

#2¶


To get started, import numpy library. NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. Also import visualization libraries matplotlib, plotly, seaborn to generate different types of plots.

In [18]:
import numpy as np
import plotly.express as px
import plotly
from matplotlib import pyplot as plt
import seaborn as sns

plotly.offline.init_notebook_mode()  # This is required to show graphs offline

Pie Chart¶

We are using matplotlib for our first sample graph. Define the properties and draw the plots

In [28]:
fig, ax = plt.subplots()

size = 0.3
vals = np.array([[60.0, 32.0], [37.0, 40.0], [29.0, 10.0]])

cmap = plt.colormaps["tab20c"]
outer_colors = cmap(np.arange(3) * 5)
inner_colors = cmap([1, 2, 6, 7, 10, 11])

ax.set(aspect="equal", title="PieChart - matplotlib")
ax.pie(
    vals.sum(axis=1),
    radius=1,
    colors=outer_colors,
    wedgeprops=dict(width=size, edgecolor="b"),
)

ax.pie(
    vals.flatten(),
    radius=1 - size,
    colors=inner_colors,
    wedgeprops=dict(width=size, edgecolor="b"),
)

plt.show()

Wave Graph¶

Creating a simple wave graph using matplotlib.

In [36]:
x = np.linspace(0, 10, 100)

fig = plt.figure()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x), "--")
Out[36]:
[<matplotlib.lines.Line2D at 0x135bd42d0>]

Bar Graph¶

For this example, using plotly library. We are using the iris dataset. The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant.

iris-image

In [40]:
df = px.data.iris()

fig = px.bar(df, x="sepal_width", y="sepal_length")

fig.show()

The dataset suggests that the plants with sepal width around 3cm and sepal length around 160 are more common

Scatter Plot¶

For final example, using seaborn to draw a scatterplot to visualize tipping amount with respect to total bill amount

In [39]:
tips = sns.load_dataset("tips")

sns.scatterplot(x="total_bill", y="tip", data=tips)
Out[39]:
<Axes: xlabel='total_bill', ylabel='tip'>